<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="渠道名称">
              <j-search-select-tag ref="searchSelectTagRef" placeholder="请输入渠道名称" v-model="queryParam.channelId" :filterOption_="false" @handleSearch="handleSearch"></j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物流商名称">
              <j-search-select-tag ref="providerNameRef" placeholder="请输入物流商名称" v-model="queryParam.providerId" :filterOption_="false" @handleSearch="handleSearch_"></j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="分区方案名称">
              <a-input placeholder="请输入分区方案名称" v-model.trim="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <!-- <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="方案状态">
                <j-dict-select-tag placeholder="请选择方案状态" v-model="queryParam.isEnable" dictCode="is_enable"/>
              </a-form-item>
            </a-col> -->
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" v-has="'logisticsPartition:add'" icon="plus">创建分区方案</a-button>

      <!-- <a-dropdown v-if="selectedRowKeys.length > 0 && isShow">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="null"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :customRow="selectRowData">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" :preview="0"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="isEnableSlot" slot-scope="text, record" >
          <span :style="{color: record.isEnable === 'Y' ? '#67c23a' : 'red'}">{{ text }}</span>
        </span>
        <span slot="remarkSlot" slot-scope="text, record">
          <j-ellipsis :value="text" :length="20" />
        </span>

        <span slot="action" slot-scope="text, record">
          <a v-has="'logisticsPartition:edit'" @click="handleEdit_(record)">编辑</a>
          <a-divider v-has="'logisticsPartition:edit'" type="vertical" />
          <!-- 下游功能是否引用 -->
          <template v-if="record.allowEdit === 'Y'">
            <a v-has="'logisticsPartition:enable'" v-if="record.isEnable === 'N'" @click="changeEnable('Y',record)">启用</a>
            <a v-has="'logisticsPartition:enable'" v-if="record.isEnable === 'Y'" @click="changeEnable('N',record)" style="color: red;">禁用</a>
            <a-divider type="vertical" v-has="'logisticsPartition:enable'" />
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <!-- <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item> -->
              <a-menu-item v-has="'logisticsPartition:config'">
                <a  @click="partitionConfig(record)">分区设置</a>
              </a-menu-item>
              <!-- 操作日志 -->
              <a-menu-item>
                <j-operate-log :id="record.id"></j-operate-log>
              </a-menu-item>
              <!-- 管理员账号，下游没引用，禁用状态 -->
              <a-menu-item  v-has="'logisticsPartition:del'" v-if="isShow && record.allowEdit === 'Y' && record.isEnable === 'N'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        <!-- 日期 -->
        <template slot="timeSlot" slot-scope="text,record">
          <div class="templateSlot">
            <div v-if="record.createTime">
              <span>创建日期：</span>
              <span>{{record.createTime}} {{record.createBy}}</span>
            </div>
            <div v-if="record.updateTime">
              <span>更新日期：</span>
              <span>{{record.updateTime}} {{record.updateBy}}</span>
            </div>
          </div>
        </template>
      </a-table>
    </div>

    <logistics-partition-modal ref="modalForm" @ok="modalFormOk"></logistics-partition-modal>
    <logistics-partition-classify-list-modal ref="modalConfigForm" @ok="modalFormOk"></logistics-partition-classify-list-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import LogisticsPartitionModal from './modules/LogisticsPartitionModal'
  import LogisticsPartitionClassifyListModal from './modules/LogisticsPartitionClassifyListModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { getAction } from '@api/manage'
  import Vue from 'vue'
  import { USER_INFO } from "@/store/mutation-types"
  import { simpleDebounce } from '@/utils/util'

  export default {
    name: 'LogisticsPartitionList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      LogisticsPartitionModal,
      LogisticsPartitionClassifyListModal,
    },
    data () {
      return {
        description: '物流分区方案管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'分区方案名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'匹配类型',
            align:"center",
            dataIndex: 'matchType_dictText'
          },
          {
            title:'物流商',
            align:"center",
            dataIndex: 'providerName'
          },
          {
            title:'物流渠道',
            align:"center",
            dataIndex: 'channelName'
          },
          {
            title:'日期',
            align:"center",
            dataIndex: 'time',
            scopedSlots: { customRender: 'timeSlot' }
          },
          {
            title:'方案状态',
            align:"center",
            dataIndex: 'isEnable_dictText',
            scopedSlots: { customRender: 'isEnableSlot' }
          },
          {
            title:'备注说明',
            align:"center",
            dataIndex: 'remark',
            scopedSlots: { customRender: 'remarkSlot' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/logistics/part/logisticsPartition/list",
          delete: "/logistics/part/logisticsPartition/delete",
          deleteBatch: "/logistics/part/logisticsPartition/deleteBatch",
          exportXlsUrl: "/logistics/part/logisticsPartition/exportXls",
          importExcelUrl: "/logistics/part/logisticsPartition/importExcel",
          queryNameOrCode: "/logistics/logisticsProvider/queryNameOrCode",
          queryNameOrCodeChannel: "/logistics/channel/queryNameOrCode"
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
      isShow(){
        return Vue.ls.get(USER_INFO).username === 'admin(A1000)';
      }
    },
    watch: {
      'queryParam.channelId': {
        immediate: true,
        handler(val){
          if(!val){
            this.handleSearch('');
          }
        }
      },
      'queryParam.providerId': {
        immediate: true,
        handler(val){
          if(!val){
            this.handleSearch_('');
          }
        }
      },
    },
    methods: {
      initDictConfig(){
      },
      selectRowData(record,index){
        const that = this
        return {
          on:{
            dblclick:() => {
              // this.handleDetail(record)
            }
          }
        }
      },
      //操作启用禁用
      changeEnable(checked,record){
        getAction('/logistics/part/logisticsPartition/changeEnable/'+record.id, {isEnable: checked})
          .then((res)=>{
            if(res.success){
              this.$message.success(res.message)
              this.loadData();
            }else{
              if(res.message.indexOf('无可用分区') > -1){
                this.$confirm({
                  title:'启用失败',
                  content: (
                    <div>
                      <div style="color: red">当前方案无可用分区无法启用</div>
                      <div>请前往分区设置处理</div>
                    </div>
                  ),
                  okText: '知道了',
                  cancelButtonProps: { style: { display: 'none' } },
                  onOk() { },
                  onCancel() {  },
                });
              }else{
                this.$message.warning(res.message)
              }
            }
          })
      },
      partitionConfig: function (record) {
        this.$refs.modalConfigForm.list(record);
        this.$refs.modalConfigForm.title = "分区配置";
        this.$refs.modalConfigForm.disableSubmit = false;
      },

      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
        fieldList.push({type:'datetime',value:'createTime',text:'创建日期'})
        fieldList.push({type:'string',value:'updateBy',text:'更新人',dictCode:''})
        fieldList.push({type:'datetime',value:'updateTime',text:'更新日期'})
        fieldList.push({type:'string',value:'name',text:'分区方案名称',dictCode:''})
        fieldList.push({type:'string',value:'matchType',text:'匹配类型',dictCode:'logistics_match_type'})
        fieldList.push({type:'string',value:'channelId',text:'物流渠道',dictCode:'hz_logistics_channel,channel_name,id'})
        fieldList.push({type:'string',value:'isEnable',text:'方案状态',dictCode:'y_n'})
        fieldList.push({type:'string',value:'remark',text:'备注说明',dictCode:''})
        this.superFieldList = fieldList
      },
      handleSearch: simpleDebounce(function(likeParam){
        getAction(this.url.queryNameOrCodeChannel, { likeParam: likeParam.trim() }).then((res) => {
          if(res.success){
            let dictOptions = res.result && res.result.map(item => {
              return {
                value: item.id,
                text: item.channelName
              }
            })
            this.$refs.searchSelectTagRef.setCurrentDictOptions(dictOptions);
          }else{
            this.$message.warning(res.message);
          }
        })
      }, 200),
      handleSearch_: simpleDebounce(function(likeParam){
        getAction(this.url.queryNameOrCode, { likeParam: likeParam.trim() }).then((res) => {
          if(res.success){
            let dictOptions = res.result && res.result.map(item => {
              return {
                value: item.id,
                text: item.name
              }
            })
            this.$refs.providerNameRef.setCurrentDictOptions(dictOptions);
          }else{
            this.$message.warning(res.message);
          }
        })
      }, 200),
      handleEdit_(record){
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        // 下游是否引用
        this.$refs.modalForm.disableSubmit = record.allowEdit === 'N' ? true : false;
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>